package com.ibeeking.found.id.rest.redis;

import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
/**
 * 默认的ID服务降级策略-采用随机数+UUID
 *
 * @author hao
 */
@Component
public class DefaultIdFallback implements IdFallback {

    @Override
    public String nextId() {
        return String.valueOf(uuid());
    }
    /**
     * 生成4位随机数+UUID hashCode的序列号
     * @return uuid
     */
    public static Long uuid() {
        int random = ThreadLocalRandom.current().nextInt(1000, 10000);
        int uuid = Math.abs(UUID.randomUUID().toString().hashCode());
        return Long.valueOf(random + String.format("%010d", uuid));
    }
}
